#include "snake.h"
#include "ui_snake.h"
#include<qrect.h>
#include<qtimer.h>
#include<QKeyEvent>
#include<QDebug>
#include<QFont>
#include<QPixmap>
Snake::Snake(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Snake),blsRun(false),speed(sp)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,300,580,620));
}

Snake::~Snake()
{
    delete ui;
}
void Snake::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!blsRun){
        InitSnake();
    }

    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(15,15,510,510);

    painter.setPen(Qt::black);
    painter.setBrush(Qt::white);
    painter.drawRect(20,20,500,500);

    painter.setPen(Qt::blue);
    for(int i=2;i<=52;i++){
        painter.drawLine(20,i*10,520,i*10);
        painter.drawLine(i*10,20,i*10,520);
    }

    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(40,570,sDisplay);

    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(140,600,scoreLabel);
    painter.drawText(230,600,QString::number(nScore));

    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawRect(food);
    painter.drawRects(&vSnakeRect[0],vSnakeRect.size());
    if(blsOver)
        timer->stop();
}
void Snake::InitSnake(){
    nDirection=2;
    blsRun=true;
    blsOver=false;
    sDisplay="游戏开始";
    scoreLabel="score：";
    nScore=0;
    food=CreateRect();
    vSnakeRect.resize(4);
    for(int i=0;i<vSnakeRect.size();i++){
        QRect rect(100,70+10*i,10,10);
        vSnakeRect[vSnakeRect.size()-1-i]=rect;
    }
    SnakeHead=vSnakeRect.first();
    timer=new QTimer(this);
    timer->start(speed);
    connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));

}
void Snake::Snake_update(){
    sDisplay="";
    SnakeHead=vSnakeRect.first();
    IsEat();
    IsHit();
    for(int j=0;j<vSnakeRect.size()-1;j++){
        vSnakeRect[vSnakeRect.size()-1-j]=vSnakeRect[vSnakeRect.size()-2-j];
    }
    switch(nDirection)
    {
    case 1:
        SnakeHead.setTop(SnakeHead.top()-10);
        SnakeHead.setBottom(SnakeHead.bottom()-10);
        break;
    case 2:
        SnakeHead.setTop(SnakeHead.top()+10);
        SnakeHead.setBottom(SnakeHead.bottom()+10);
        break;
    case 3:
        SnakeHead.setLeft(SnakeHead.left()-10);
        SnakeHead.setRight(SnakeHead.right()-10);
        break;
    case 4:
        SnakeHead.setLeft(SnakeHead.left()+10);
        SnakeHead.setRight(SnakeHead.right()+10);
    default:;


    }
    vSnakeRect[0]=SnakeHead;
    if(SnakeHead.left()<20||SnakeHead.right()>520||SnakeHead.top()<20 || SnakeHead.bottom()>520){
        sDisplay="撞到墙壁，游戏结束 ！";
        blsOver=true;
    }

        update();
}
void Snake::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*)event;
    switch (key->key()) {
    case Qt::Key_Up:nDirection=1;
        break;
    case Qt::Key_Down:nDirection=2;
        break;
    case Qt::Key_Left:nDirection=3;
        break;
    case Qt::Key_Right:nDirection=4;
        break;
    default:;
    }
}
QRect Snake::CreateRect(){
    int x,y;
    x=qrand()%50;
    y=qrand()%50;
    QRect rect(20+x*10,20+y*10,10,10);
    return rect;
}
void Snake::IsEat(){
    if(SnakeHead==food){
         SnakeHead=food;
         vSnakeRect.push_back(vSnakeRect.last());
         food=CreateRect();
         nScore+=1;
         if(nScore%2==0&&sp>=10){sp-=10;}
         if(speed>50){
             speed=speed-10;
             timer->stop();
             timer->start(speed);
         }
    }

}
void Snake::IsHit(){
    for(int i=1;i<vSnakeRect.size();i++){
        if(SnakeHead==vSnakeRect[i]){
            sDisplay="撞到自身，游戏结束 ！";
            blsOver=true;
            update();
        }
    }
}
